import numpy as np 
count = 0   #全局变量，用于计算匹配度达0.90的次数

#换算向量中心点
def vector_centerpoint(matrix):
    sum1 = sum2 = 0
    for i in range(0,len(matrix),2):
        sum1 += matrix[i]
    average1 = float(sum1 / 19)
    for i in range(0,len(matrix),2):
        matrix[i] = matrix[i] - average1

    for j in range(1,len(matrix),2):
        sum2 += matrix[j]
    average2 = float(sum2 / 19)
    for j in range(1,len(matrix),2):
        matrix[j] = matrix[j] - average2
    return matrix

# 计算余弦相似度
def similarity(matrix1,matrix2):
    import math
    sum = 0
    sq1 = 0
    sq2 = 0
    for i in range(len(matrix1)):
	    sum += matrix1[i] * matrix2[i]
	    sq1 += pow(matrix1[i], 2)
	    sq2 += pow(matrix2[i], 2)
    
    try:
	    result = round(float(sum) / (math.sqrt(sq1) * math.sqrt(sq2)), 2)
    except ZeroDivisionError:
        result = 0.0

    return result

#改进版相似度达0.9播放视频
def video_brodcast(matrix1,matrix2):
    global count           #声明使用全局变量
    #Flag = True
    #result = similarity(matrix1,matrix2)
    #while Flag:
    result = similarity(matrix1,matrix2)#计算相似度
    # print(result)
    if result >= 0.20 and result <= 1.00:#判断相似度是否达0.90
        count += 1                       #若相似度达0.90，count加1
    else:
        count = 0
    #若count为3，即连续3次检测到动作匹配度为0.90，播放视频
    if count == 3:
        count = 0      #count重置为3，作为下一次播放视频的判断初值
        return 1






        #Flag = False
        # class Video(object):
        #     def __init__(self,path):
        #         self.path = path

        #     def play(self):
        #         from os import startfile
        #         startfile(self.path)

        # class Movie_MP4(Video):
        #     type = "MP4"
        # movie = Movie_MP4(r"C:\Users\ASUS\Desktop\半成品 - 副本\1.mp4")
        # #if raw_input("Press enter to play, anything else to exit") == '':
        # movie.play()
    #else:    
           #Flag = True

